package com.realgodo.site.entity.application.vote;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.realgodo.site.entity.IdEntity;

/**
 * 投票项
 * @author bijiqiang E-mail: bijiqiang@gmail.com
 * @version 创建时间：2009-12-13下午03:17:06
 * @since jdk1.5
 * @version 0.1
 */
@Entity(name="VoteItem")
@Table(name="vote_item")
public class VoteItem extends IdEntity {

	private static final long serialVersionUID = 1994946289973605824L;
	
	private String title;
	private Long voteCount = 0L;
	private Vote vote;

	@Column(name = "title")
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(name = "vote_count")
	public Long getVoteCount() {
		return voteCount;
	}

	public void setVoteCount(Long voteCount) {
		this.voteCount = voteCount;
	}
	
	@ManyToOne()
	@JoinColumn(name = "vote_id", referencedColumnName = "id", nullable = false)
	public Vote getVote() {
		return vote;
	}

	public void setVote(Vote vote) {
		this.vote = vote;
	}

	@Transient
	public int getPercent() {
		Long count = this.vote.getTotalCount();
		if (count == 0L) {
			return 0;
		} else {
			int tmp = Math.round((this.voteCount.floatValue() / count
					.floatValue()) * 100);
			return tmp;
		}
	}
}
